from rest_framework import serializers
from .models import StudyRecord
from apps.users.serializers import UserSerializer

class StudyRecordSerializer(serializers.ModelSerializer):
    user_info = UserSerializer(source='user', read_only=True)

    class Meta:
        model = StudyRecord
        fields = ['id', 'user', 'user_info', 'category', 'duration', 'notes', 'start_time', 'created_at']
        read_only_fields = ['created_at', 'user_info']

    def create(self, validated_data):
        request = self.context.get('request')
        if request and request.data.get('user_id'):
            validated_data['user_id'] = request.data.get('user_id')
        return super().create(validated_data)